#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
#define m 6
#define n 8
typedef struct
{
int x;
int y;
int d;
} datatype;
typedef struct
{
datatype data[maxsize];
int top;
} seqstack;
typedef struct
{
int x;
int y;
} item;
seqstack* init (seqstack*l)
{
l=(seqstack*)malloc(sizeof(seqstack));
l->top=-1;
return l;
}
void push_stack(seqstack*s,datatype temp)
{
if(s->top!=maxsize-1)
{
s->top++;
s->data[s->top].x=temp.x;
s->data[s->top].y=temp.y;
s->data[s->top].d=temp.d;
}
}
void pop_stack(seqstack*s,datatype* temp)
{
if(s->top!=-1)
{
temp->d=s->data[s->top].d;
temp->y=s->data[s->top].y;
temp->x=s->data[s->top].x;
s->top--;
}
}
void path(int maze[m+2][n+2],item move[8])
{
seqstack *s;
int k;
int x,y,i,j,v;
datatype temp;
s=init(s);
temp.x=1;
temp.y=1;
temp.d=-1;
push_stack(s,temp);
while(s->top!=-1)
{
pop_stack(s,&temp);
x=temp.x;
y=temp.y;
v=temp.d+1;
while(v<8)//若还有方向可走,八个方向遍历
{
i=x+move[v].x;
j=y+move[v].y;//移动后新坐标点
if(maze[i][j]==0)
{
temp.x=x;
temp.y=y;
temp.d=v;//保存前一个点的位置
push_stack(s,temp);
x=i;
y=j;
maze[x][y]=-1;//避免进入死循环,将走过的点做标记
if(m==i&&n==j)
{
temp.x=i;
temp.y=j;
temp.d=v;
push_stack(s,temp);//已到目标点,将新点即目标点入栈
for(k=0; k<=s->top; k++)
{
printf("\t(%d,%d)\t",s->data[k].x,s->data[k].y);
printf("\n");
}
}
else v=0;
}
else v++;//换方向
}
}
}
int main()
{
int maze[8][10]= {
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,1,0,1,1,1,1,1},
{1,0,1,0,0,0,0,0,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,0,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
item move[8]= {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
path(maze,move);
return 0;
}